<template>
  <div class="comment-item-wrap">
      <van-cell class="comment-item-cell">
          <template #title>
              <van-image round fit='cover' class="user-photo"
              :src="article.aut_photo" />
              <span>{{article.aut_name}}</span>
          </template>
          <template #value>
              <div @click="onCommentLike">
              <i class="iconfont icon-dianzan2" :class="{isLiked:article.is_liking}"></i>
              <span>{{article.like_count>0 ? article.like_count : '赞'}}</span>
              </div>
          </template>
      </van-cell>
      <div class="comment-content">{{article.content}}</div>
      <div class="comment-item-bottom">
          <span class="time">{{relativeTime}}</span>
          <span class="back" @click="$emit('reply-click',article)">回复 {{article.reply_count}}</span>
      </div>
  </div>
</template>

<script>
import time from '@/utils/day.js'
import { addCommentLike, deleteCommentLike } from '../api/comment'
export default {
  name: 'ArticleItem',
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  data () {
    return {}
  },
  computed: {
    relativeTime () {
      return time(this.article.pubdate)
    }
  },
  methods: {
    async onCommentLike () {
      try {
        if (this.article.is_liking) {
          // 已经点赞了，点击取消点赞
          await deleteCommentLike(this.article.com_id)
          // 评论数减一
          this.$emit('delete-comment-like')
        } else {
          // 点击点赞
          await addCommentLike({ target: this.article.com_id })
          // 评论数加一
          this.$emit('add-comment-like')
        }
      } catch (error) {
        console.log(error)
      }
      this.$emit('update-comment-like', !this.article.is_liking)
    }
  }
}
</script>

<style scoped lang="less">
.comment-item-wrap {
    /deep/.comment-item-cell {
        background-color: unset;
        .van-cell__title {
            display: flex;
            .user-photo {
              width: 80px;
              height: 80px;
            }
            img {
                width: 80px;
                height: 80px;
            }
            span {
                margin-bottom: 20px;
                color: rgb(39, 167, 167);
                font-size: 28px;
            }
        }
        .van-cell__value {
            line-height: 80px;
            i {
                font-size: 30px;
            }
            .isLiked {
                color: rgb(248, 5, 5);
            }
        }   span {
            padding-left: 15px;
        }
    }
    .comment-content {
        font-size: 32px;
        padding-left: 122px;
        margin: 10px 0;
        color: rgb(0, 0, 0);
    }
    .comment-item-bottom {
        font-size: 26px;
        background-color: rgb(199, 47, 199);
        span {
            display: block;
            float: left;
            height: 50px;
            text-align: center;
            line-height: 50px;
        }
        span.time {
            margin-left: 102px;
            margin-right: 50px;
        }
    }
}
</style>
